/**
 * Created by hukekuan on 2017/12/14.
 */
var dataCache={}; 
(function(){
    var isWinRT = (typeof Windows === "undefined") ? false : true;
    var r = new RegExp("(^|(.*?\\/))(hbgis\.js)(\\?|$)"),
        s = document.getElementsByTagName('script'),
        src, m,baseurl = "",serverName;
    for(var i=0, len=s.length; i<len; i++) {
        src = s[i].getAttribute('src');
        if(src) {
            m = src.match(r);
            if(m) {
                baseurl = m[1];
                if(baseurl.split('static')[0] != ""){
                    serverName = baseurl.split('static')[0];

                    if(src.indexOf("?") > 0){
                        var paramsArray = src.split("?")[1].split("&"),keyStr;
                        for(var j = 0,jLen = paramsArray.length;j < jLen;j++){
                            if(paramsArray[j].indexOf("key") == 0){
                                keyStr = paramsArray[j].split("=")[1]
                                $.data(dataCache, 'gisSupportUrl', serverName);
								$.data(dataCache, 'key', keyStr);
                                break;
                            }
                        }
                    }
                }
                break;
            }
        }
    }


    function inputScript(inc){
        if (!isWinRT) {
            var script = '<' + 'script type="text/javascript" src="' + inc + '"' + '><' + '/script>';
            document.writeln(script);
        } else {
            var script = document.createElement("script");
            script.src = inc;
            document.getElementsByTagName("HEAD")[0].appendChild(script);
        }
    }
    function inputCSS(style){
        if (!isWinRT) {
            var css = '<' + 'link rel="stylesheet" href="' + style + '"' + '><' + '/>';
            document.writeln(css);
        } else {
            var link = document.createElement("link");
            link.rel = "stylesheet";
            link.href = style;
            document.getElementsByTagName("HEAD")[0].appendChild(link);
        }
    }

    inputCSS(serverName + "static/libs/ol4.5.0/ol.css");
    inputCSS(serverName + "static/spatial/css/popup.css");
    // inputScript(serverName + "static/libs/jsviews.min.js");
    inputScript(serverName + "static/libs/ol4.5.0/ol.js");
    inputScript(serverName + "static/libs/iclient9-openlayers/iclient-openlayers.min-9.0.0-742.js");
    inputScript(serverName + "static/libs/FileSaver.min.js");
    inputScript(serverName + "static/spatial/js/c3gis.js");

    // inputScript(baseurl + "libs/encrypt/tripledes.js");
    // inputScript(baseurl + "libs/encrypt/mode-ecb.js");
    // inputScript(baseurl + "libs/encrypt/md5.js");

    function decryptByDES(ciphertext, key) {
        if(ciphertext == null){
            return null;
        }
        var keyHex = CryptoJS.enc.Utf8.parse(key);
        var decrypted = CryptoJS.DES.decrypt({
            ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
        }, keyHex, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return decrypted.toString(CryptoJS.enc.Utf8);
    }
})();
